# Python traceback模块：获取异常信息
'''
class SelfException(Exception):
    pass

def main():
    firstMethod()

def firstMethod():
    secondMethod()

def secondMethod():
    thirdMethod()

def thirdMethod():
    raise SelfException("自定义异常信息")

main()
'''

print()

# 导入 traceback 模块
import traceback
class SelfException(Exception):
    pass

def main():
    firstMethod()

def firstMethod():
    secondMethod()

def secondMethod():
    thirdMethod()

def thirdMethod():
    raise SelfException("自定义异常信息")

try:
    main()
except:
    # 捕捉异常，并将异常传播信息输出控制台
    traceback.print_exc()
    # 捕捉异常，并交异常传播信息输出指定文件中
    traceback.print_exc(file = open('../logs/log.txt', 'a'))
